package com.example.hanfu_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hanfu_backend.entity.Goods;
import com.example.hanfu_backend.entity.Hanfu;
import com.example.hanfu_backend.mapper.GoodsMapper;
import com.example.hanfu_backend.mapper.HanfuMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class GoodsController {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private HanfuMapper hanfuMapper;

    @PostMapping("/insertOrder")
    @Operation(summary = "添加到订单")
    public int insertOrder(@RequestBody Goods order) {
        // 汉服数量减少
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_id",order.getHanfuId());
        Hanfu hanfu = hanfuMapper.selectOne(queryWrapper);
        hanfu.setHanfuInventory(hanfu.getHanfuInventory() - order.getOrderSum());
        if(hanfu.getHanfuInventory() == 0) hanfu.setHanfuStatu(0); // 库存为0 下架
        hanfuMapper.updateById(hanfu);
        return goodsMapper.insert(order);
    }

    @GetMapping("/getOneOrder")
    @Operation(summary = "得到某个用户的订单")
    public List<Map<String,Object>> getAOrder(@RequestParam("customerId")Integer customerId) {
        return goodsMapper.getOneOrder(customerId);
    }

    @GetMapping("/getAllOrder")
    @Operation(summary = "分页得到所有用户订单")
    public List<Map<String,Object>> getAllOrder(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        Page<Map<String,Object>> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Map<String,Object>> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("order_id");
        List<Map<String,Object>> iPage = goodsMapper.getAllOrder(page,queryWrapper);
        return iPage;
    }

    @GetMapping("/getAllOrderNoPage")
    @Operation(summary = "不分页得到所有用户订单")
    public List<Map<String,Object>> getAllOrderNoPage() {
        QueryWrapper<Map<String,Object>> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("order_id");
        List<Map<String,Object>> iPage = goodsMapper.getAllOrderNoPage();
        return iPage;
    }

    @GetMapping("/getTopTen")
    @Operation(summary = "得到热销前十件产品")
    public List<Map<String,Object>> getTopTen() {
        return goodsMapper.getTopTen();
    }

    @GetMapping("/getOrderSum")
    @Operation(summary = "得到订单数和金额")
    public Map<String,Object> getOrderSum() {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        // 获取订单数
        Long orderCount = goodsMapper.selectCount(queryWrapper);
        // 获取总金额
        queryWrapper.select("SUM(order_price) as totalAmount");
        Map<String, Object> result = goodsMapper.selectMaps(queryWrapper).get(0);
        double totalAmount = Double.parseDouble(result.get("totalAmount").toString());
        Map<String, Object> summary = new HashMap<>();
        summary.put("orderCount", orderCount);
        summary.put("totalAmount", totalAmount);
        return summary;
    }
}
